package com.translate.web.api.controller;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import com.translate.vo.ChangeRecordVO;
import com.response.vo.ResponseModel;
import com.controller.base.AbstractCommonController;
import com.jwt.IJwtUtil;
import com.translate.web.param.ChangeRecordUpdateParam;
import com.translate.web.param.DateRangeParam;
import com.translate.web.param.UserIdParam;
import com.translate.web.param.ChangeRecordAddParam;
import com.translate.web.param.contract.IChangeRecordAddParam;
import com.translate.entity.ChangeRecord;
import com.translate.web.service.IChangeRecordService;
import com.basic.service.contract.IBaseService;

@RestController
@RequestMapping("changeRecord")
public class ChangeRecordController extends AbstractCommonController<ChangeRecord, DateRangeParam> {
	private IChangeRecordService service;
	private IJwtUtil jwtUtil;

	@Autowired
	public void setJwtUtil(IJwtUtil jwtUtil) {
		this.jwtUtil = jwtUtil;
	}

	public ChangeRecordController(IChangeRecordService service) {
		this.service = service;
	}

	protected long setCreateInfo(IChangeRecordAddParam param, HttpServletRequest httpServletRequest) {
		long userId = this.getUserId(httpServletRequest);
//		param.setCreateUserId((int) userId);
		return userId;
	}

	protected long getUserId(HttpServletRequest httpServletRequest) {
		return jwtUtil.getId(httpServletRequest.getHeader(jwtUtil.getJwtConfig().getHeader()));
	}

	
	@Override
	protected void frontProcesserVO(DateRangeParam param, NativeWebRequest object) {
		// TODO Auto-generated method stub
		super.frontProcesserVO(param, object);
		
		param.setUserId((int) this.getUserId(object));
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/add")
	public ResponseModel<Integer> insert(@RequestBody @Validated ChangeRecordAddParam param,
			HttpServletRequest httpServletRequest) {
		setCreateInfo(param, httpServletRequest);
		int id = this.service.parseInsert(param, ChangeRecord.class);

		return ResponseModel.<Integer>success(id);
	}

	@Override
	protected IBaseService<ChangeRecord> getBaseService() {
		return this.service;
	}

	/**
	 * 添加
	 * 
	 * @param <V>
	 * @param param
	 * @return
	 */
	@PostMapping("/update")
	public ResponseModel<Integer> update(@Validated @RequestBody ChangeRecordUpdateParam param,
			HttpServletRequest httpServletRequest) {
		int res = this.service.parseUpdate(param, ChangeRecord.class);
		return ResponseModel.<Integer>success(res);
	}

	protected <VO> Class<VO> getTarget() {
		return (Class<VO>) ChangeRecordVO.class;
	}
}